<?php

namespace App\Http\Controllers;

use App\Service\BMR\BMR_Metabolize;
use App\Service\BMR\Metabolize\BasialMetabolize;
use App\Service\BMR\Metabolize\Man;
use App\Service\BMR\Metabolize\Woman;
use App\Service\BMR\Nutrition\Carbohydrate;
use App\Service\BMR\Nutrition\Fat;
use App\Service\BMR\Nutrition\Protein;
use App\Service\FoodService;
use App\Service\LoginService;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    protected $login = false;
    //登录页
    public function index() {
        if ($this->getLoginUser()) {
            return redirect('/calculate/count');
        }
        return view('bmr/login');
    }

    public function login(Request $request) {
        if ($request->isMethod('post')) {
            $inputs = $request->all(['phone', 'name']);
            $loginService = new LoginService($inputs);
            //登录失败
            if (!$loginService->action()) {
                return view('bmr/login', $inputs);
            }
            //登录成功
            $this->setLoginUser($loginService->getUser());
            //跳转评测中心
            return redirect('/calculate/count');
        }
        return view('bmr/login');
    }

    /**登出
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function logout() {
        $this->destroyUser();
        return redirect('/');
    }
}